home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / XML Utilities / Professional Programmer XSL IDE / Xselerator25.msi / Data.Cab / F44566_trig_lib.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2002-03-14  |  14.8 KB  |  371 lines

  1. <?xml version="1.0"?>
  2. <!--
  3. ==========================================================================
  4.  Stylesheet: trig_lib.xsl
  5.     Version: 1.0 (2002-03-13)
  6.      Author: Dimitre Novatchev
  7.              (optimization by Martin "Marrow" Rowlinson)
  8.      Notice: Copyright (c)2002 D.Novatchev  ALL RIGHTS RESERVED.
  9.              No limitation on use - except this code may not be published,
  10.              in whole or in part, without prior written consent of the
  11.              copyright owner.
  12. ========================================================================== -->
  13. <xsl:stylesheet version="1.0" exclude-result-prefixes="xsl trigSin trigCos trigTan trigCot trigSec trigCsc"
  14.   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  15.   xmlns:trigSin="f:trigSin" xmlns:trigCos="f:trigCos"
  16.   xmlns:trigTan="f:trigTan" xmlns:trigCot="f:trigCot"
  17.   xmlns:trigSec="f:trigSec" xmlns:trigCsc="f:trigCsc">
  18.  
  19.   <!--
  20.   ==========================================================================
  21.     Template: sin
  22.      Purpose: Return the sine of X
  23.   Parameters:-
  24.     $pX    - the angle (specified in radians or degrees - see $pUnit)
  25.     $pUnit - [optional] the unit of the given angle ┬úpX
  26.              specify 'deg' for degrees or
  27.                      'rad' for radians (default)
  28.     $pEps  - [optional] accuracy required
  29.              increase the number of decimal places for greater accuracy but
  30.              at the expense of performance.
  31.   ========================================================================== -->
  32.   <xsl:template name="sin">
  33.     <xsl:param name="pX"/>
  34.     <xsl:param name="pUnit" select="'rad'"/>
  35.     <xsl:param name="pEps" select=".00000001"/>
  36.     <xsl:call-template name="_trigWrapper">
  37.       <xsl:with-param name="pFun" select="$vstTop/trigSin:*[1]"/>
  38.       <xsl:with-param name="pX" select="$pX"/>
  39.       <xsl:with-param name="pUnit" select="$pUnit"/>
  40.       <xsl:with-param name="pEps" select="$pEps"/>
  41.     </xsl:call-template>
  42.   </xsl:template>
  43.  
  44.   <!--
  45.   ==========================================================================
  46.     Template: cos
  47.      Purpose: Return the cosine of X
  48.   Parameters:-
  49.     $pX    - the angle (specified in radians or degrees - see $pUnit)
  50.     $pUnit - [optional] the unit of the given angle ┬úpX
  51.              specify 'deg' for degrees or
  52.                      'rad' for radians (default)
  53.     $pEps  - [optional] accuracy required
  54.              increase the number of decimal places for greater accuracy but
  55.              at the expense of performance.
  56.   ========================================================================== -->
  57.   <xsl:template name="cos">
  58.     <xsl:param name="pX"/>
  59.     <xsl:param name="pUnit" select="'rad'"/>
  60.     <xsl:param name="pEps" select=".00000001"/>
  61.     <xsl:call-template name="_trigWrapper">
  62.       <xsl:with-param name="pFun" select="$vstTop/trigCos:*[1]"/>
  63.       <xsl:with-param name="pX" select="$pX"/>
  64.       <xsl:with-param name="pUnit" select="$pUnit"/>
  65.       <xsl:with-param name="pEps" select="$pEps"/>
  66.     </xsl:call-template>
  67.   </xsl:template>
  68.  
  69.   <!--
  70.   ==========================================================================
  71.     Template: tan
  72.      Purpose: Return the tangent of X
  73.   Parameters:-
  74.     $pX    - the angle (specified in radians or degrees - see $pUnit)
  75.     $pUnit - [optional] the unit of the given angle ┬úpX
  76.              specify 'deg' for degrees or
  77.                      'rad' for radians (default)
  78.     $pEps  - [optional] accuracy required
  79.              increase the number of decimal places for greater accuracy but
  80.              at the expense of performance.
  81.   ========================================================================== -->
  82.   <xsl:template name="tan">
  83.     <xsl:param name="pX"/>
  84.     <xsl:param name="pUnit" select="'rad'"/>
  85.     <xsl:param name="pEps" select=".00000001"/>
  86.     <xsl:call-template name="_trigWrapper">
  87.       <xsl:with-param name="pFun" select="$vstTop/trigTan:*[1]"/>
  88.       <xsl:with-param name="pX" select="$pX"/>
  89.       <xsl:with-param name="pUnit" select="$pUnit"/>
  90.       <xsl:with-param name="pEps" select="$pEps"/>
  91.     </xsl:call-template>
  92.   </xsl:template>
  93.  
  94.   <!--
  95.   ==========================================================================
  96.     Template: cot
  97.      Purpose: Return the cotangent of X
  98.   Parameters:-
  99.     $pX    - the angle (specified in radians or degrees - see $pUnit)
  100.     $pUnit - [optional] the unit of the given angle ┬úpX
  101.              specify 'deg' for degrees or
  102.                      'rad' for radians (default)
  103.     $pEps  - [optional] accuracy required
  104.              increase the number of decimal places for greater accuracy but
  105.              at the expense of performance.
  106.   ========================================================================== -->
  107.   <xsl:template name="cot">
  108.     <xsl:param name="pX"/>
  109.     <xsl:param name="pUnit" select="'rad'"/>
  110.     <xsl:param name="pEps" select=".00000001"/>
  111.     <xsl:call-template name="_trigWrapper">
  112.       <xsl:with-param name="pFun" select="$vstTop/trigCot:*[1]"/>
  113.       <xsl:with-param name="pX" select="$pX"/>
  114.       <xsl:with-param name="pUnit" select="$pUnit"/>
  115.       <xsl:with-param name="pEps" select="$pEps"/>
  116.     </xsl:call-template>
  117.   </xsl:template>
  118.  
  119.   <!--
  120.   ==========================================================================
  121.     Template: sec
  122.      Purpose: Return the secant of X
  123.   Parameters:-
  124.     $pX    - the angle (specified in radians or degrees - see $pUnit)
  125.     $pUnit - [optional] the unit of the given angle ┬úpX
  126.              specify 'deg' for degrees or
  127.                      'rad' for radians (default)
  128.     $pEps  - [optional] accuracy required
  129.              increase the number of decimal places for greater accuracy but
  130.              at the expense of performance.
  131.   ========================================================================== -->
  132.   <xsl:template name="sec">
  133.     <xsl:param name="pX"/>
  134.     <xsl:param name="pUnit" select="'rad'"/>
  135.     <xsl:param name="pEps" select=".00000001"/>
  136.     <xsl:call-template name="_trigWrapper">
  137.       <xsl:with-param name="pFun" select="$vstTop/trigSec:*[1]"/>
  138.       <xsl:with-param name="pX" select="$pX"/>
  139.       <xsl:with-param name="pUnit" select="$pUnit"/>
  140.       <xsl:with-param name="pEps" select="$pEps"/>
  141.     </xsl:call-template>
  142.   </xsl:template>
  143.  
  144.   <!--
  145.   ==========================================================================
  146.     Template: csc
  147.      Purpose: Return the cosecant of X
  148.   Parameters:-
  149.     $pX    - the angle (specified in radians or degrees - see $pUnit)
  150.     $pUnit - [optional] the unit of the given angle ┬úpX
  151.              specify 'deg' for degrees or
  152.                      'rad' for radians (default)
  153.     $pEps  - [optional] accuracy required
  154.              increase the number of decimal places for greater accuracy but
  155.              at the expense of performance.
  156.   ========================================================================== -->
  157.   <xsl:template name="csc">
  158.     <xsl:param name="pX"/>
  159.     <xsl:param name="pUnit" select="'rad'"/>
  160.     <xsl:param name="pEps" select=".00000001"/>
  161.     <xsl:call-template name="_trigWrapper">
  162.       <xsl:with-param name="pFun" select="$vstTop/trigCsc:*[1]"/>
  163.       <xsl:with-param name="pX" select="$pX"/>
  164.       <xsl:with-param name="pUnit" select="$pUnit"/>
  165.       <xsl:with-param name="pEps" select="$pEps"/>
  166.     </xsl:call-template>
  167.   </xsl:template>
  168.  
  169.   <!-- defined constant for pi -->
  170.   <xsl:variable name="pi" select="3.1415926535897"/>
  171.  
  172.   <!-- ************************************************************* -->
  173.   <!-- ********************* INTERNAL USE ONLY ********************* -->
  174.   <!-- ************************************************************* -->
  175.   <!-- defined constants -->
  176.   <xsl:variable name="halfPi" select="$pi div 2"/>
  177.   <xsl:variable name="twicePi" select="$pi*2"/>
  178.   <xsl:variable name="deg2rads" select="$pi div 180"/>
  179.   <!-- internal use only - for applying functions from central _trigWrapper -->
  180.   <trigSin:trigSin/>
  181.   <trigCos:trigCos/>
  182.   <trigTan:trigTan/>
  183.   <trigCot:trigCot/>
  184.   <trigSec:trigSec/>
  185.   <trigCsc:trigCsc/>
  186.   <xsl:variable name="vstTop" select="document('')/*"/>
  187.  
  188.   <!-- internally used templates -->
  189.   <xsl:template name="_trigWrapper">
  190.     <xsl:param name="pFun" select="/.."/>
  191.     <xsl:param name="pX"/>
  192.     <xsl:param name="pUnit" select="'rad'"/>
  193.     <xsl:param name="pEps" select=".00000001"/>
  194.     <!-- convert degrees to radians (when 'deg' specified) -->
  195.     <xsl:variable name="vRads" select="(($pUnit = 'rad') * $pX) + ((not($pUnit = 'rad')) * ($pX * $deg2rads))"/>
  196.     <!-- apply the appropriate function -->
  197.     <xsl:apply-templates select="$pFun">
  198.       <xsl:with-param name="pX" select="$vRads"/>
  199.       <xsl:with-param name="pEps" select="$pEps"/>
  200.     </xsl:apply-templates>
  201.   </xsl:template>
  202.  
  203.   <xsl:template name="_sin" match="trigSin:*">
  204.     <xsl:param name="pX"/>
  205.     <xsl:param name="pEps" select=".00000001"/>
  206.     <xsl:variable name="pY">
  207.       <xsl:choose>
  208.         <xsl:when test="not(0 <= $pX and $twicePi > $pX)">
  209.           <xsl:call-template name="_cutIntervals">
  210.             <xsl:with-param name="pLength" select="$twicePi"/>
  211.             <xsl:with-param name="pX" select="$pX"/>
  212.           </xsl:call-template>
  213.         </xsl:when>
  214.         <xsl:otherwise>
  215.           <xsl:value-of select="$pX"/>
  216.         </xsl:otherwise>
  217.       </xsl:choose>
  218.     </xsl:variable>
  219.     <xsl:call-template name="_sineIter">
  220.       <xsl:with-param name="pX2" select="$pY*$pY"/>
  221.       <xsl:with-param name="pRslt" select="$pY"/>
  222.       <xsl:with-param name="pElem" select="$pY"/>
  223.       <xsl:with-param name="pN" select="1"/>
  224.       <xsl:with-param name="pEps" select="$pEps"/>
  225.     </xsl:call-template>
  226.   </xsl:template>
  227.  
  228.   <xsl:template name="_sineIter">
  229.     <xsl:param name="pX2"/>
  230.     <xsl:param name="pRslt"/>
  231.     <xsl:param name="pElem"/>
  232.     <xsl:param name="pN"/>
  233.     <xsl:param name="pEps"/>
  234.     <xsl:variable name="vnextN" select="$pN+2"/>
  235.     <xsl:variable name="vnewElem" select="-$pElem*$pX2 div ($vnextN*($vnextN - 1))"/>
  236.     <xsl:variable name="vnewResult" select="$pRslt + $vnewElem"/>
  237.     <xsl:variable name="vdiffResult" select="$vnewResult - $pRslt"/>
  238.     <xsl:choose>
  239.       <xsl:when test="$vdiffResult > $pEps or $vdiffResult < -$pEps">
  240.         <xsl:call-template name="_sineIter">
  241.           <xsl:with-param name="pX2" select="$pX2"/>
  242.           <xsl:with-param name="pRslt" select="$vnewResult"/>
  243.           <xsl:with-param name="pElem" select="$vnewElem"/>
  244.           <xsl:with-param name="pN" select="$vnextN"/>
  245.           <xsl:with-param name="pEps" select="$pEps"/>
  246.         </xsl:call-template>
  247.       </xsl:when>
  248.       <xsl:otherwise>
  249.         <xsl:value-of select="$vnewResult"/>
  250.       </xsl:otherwise>
  251.     </xsl:choose>
  252.   </xsl:template>
  253.  
  254.   <xsl:template name="_cos" match="trigCos:*">
  255.     <xsl:param name="pX"/>
  256.     <xsl:param name="pEps" select=".00000001"/>
  257.     <xsl:call-template name="_sin">
  258.       <xsl:with-param name="pX" select="$halfPi - $pX"/>
  259.       <xsl:with-param name="pEps" select="$pEps"/>
  260.     </xsl:call-template>
  261.   </xsl:template>
  262.  
  263.   <xsl:template name="_tan" match="trigTan:*">
  264.     <xsl:param name="pX"/>
  265.     <xsl:param name="pEps" select=".00000001"/>
  266.     <xsl:param name="_pAbort" select="1"/>
  267.     <xsl:variable name="vnumHalfPis" select="floor($pX div $halfPi)"/>
  268.     <xsl:variable name="vdiffHalfPi" select="$pX - $halfPi*$vnumHalfPis"/>
  269.     <xsl:choose>
  270.       <xsl:when test="-$pEps < $vdiffHalfPi and $vdiffHalfPi < $pEps
  271.                       and $vnumHalfPis mod 2 = 1">
  272.         <xsl:choose>
  273.           <xsl:when test="$_pAbort">
  274.             <xsl:message terminate="yes">
  275.               <xsl:value-of select="concat('[Error]tan() not defined for x=',$pX)"/>
  276.             </xsl:message>
  277.           </xsl:when>
  278.           <xsl:otherwise>Infinity</xsl:otherwise>
  279.         </xsl:choose>
  280.       </xsl:when>
  281.       <xsl:otherwise>
  282.         <xsl:variable name="vSin">
  283.           <xsl:call-template name="_sin">
  284.             <xsl:with-param name="pX" select="$pX"/>
  285.             <xsl:with-param name="pEps" select="$pEps"/>
  286.           </xsl:call-template>
  287.         </xsl:variable>
  288.         <xsl:variable name="vCos">
  289.           <xsl:call-template name="_cos">
  290.             <xsl:with-param name="pX" select="$pX"/>
  291.             <xsl:with-param name="pEps" select="$pEps"/>
  292.           </xsl:call-template>
  293.         </xsl:variable>
  294.         <xsl:value-of select="$vSin div $vCos"/>
  295.       </xsl:otherwise>
  296.     </xsl:choose>
  297.   </xsl:template>
  298.  
  299.   <xsl:template name="_cot" match="trigCot:*">
  300.     <xsl:param name="pX"/>
  301.     <xsl:param name="pEps" select=".00000001"/>
  302.     <xsl:variable name="vTan">
  303.       <xsl:call-template name="_tan">
  304.         <xsl:with-param name="pX" select="$pX"/>
  305.         <xsl:with-param name="_pAbort" select="0"/>
  306.       </xsl:call-template>
  307.     </xsl:variable>
  308.     <xsl:choose>
  309.       <xsl:when test="$vTan = 'Infinity'">0</xsl:when>
  310.       <xsl:when test="-$pEps < $vTan and $vTan < $pEps">
  311.         <xsl:message terminate="yes">
  312.           <xsl:value-of select="concat('[Error]cot() not defined for x=',$pX)"/>
  313.         </xsl:message>
  314.       </xsl:when>
  315.       <xsl:otherwise>
  316.         <xsl:value-of select="1 div $vTan"/>
  317.       </xsl:otherwise>
  318.     </xsl:choose>
  319.   </xsl:template>
  320.  
  321.   <xsl:template name="_sec" match="trigSec:*">
  322.     <xsl:param name="pX"/>
  323.     <xsl:param name="pEps" select=".00000001"/>
  324.     <xsl:variable name="vCos">
  325.       <xsl:call-template name="_cos">
  326.         <xsl:with-param name="pX" select="$pX"/>
  327.         <xsl:with-param name="pEps" select="$pEps"/>
  328.       </xsl:call-template>
  329.     </xsl:variable>
  330.     <xsl:choose>
  331.       <xsl:when test="-$pEps < $vCos and $vCos < $pEps">
  332.         <xsl:message terminate="yes">
  333.           <xsl:value-of select="concat('[Error]sec() not defined for x=',$pX)"/>
  334.         </xsl:message>
  335.       </xsl:when>
  336.       <xsl:otherwise>
  337.         <xsl:value-of select="1 div $vCos"/>
  338.       </xsl:otherwise>
  339.     </xsl:choose>
  340.   </xsl:template>
  341.  
  342.   <xsl:template name="_csc" match="trigCsc:*">
  343.     <xsl:param name="pX"/>
  344.     <xsl:param name="pEps" select=".00000001"/>
  345.     <xsl:call-template name="_sec">
  346.       <xsl:with-param name="pX" select="$halfPi - $pX"/>
  347.       <xsl:with-param name="pEps" select="$pEps"/>
  348.     </xsl:call-template>
  349.   </xsl:template>
  350.  
  351.   <xsl:template name="_cutIntervals">
  352.     <xsl:param name="pLength"/>
  353.     <xsl:param name="pX"/>
  354.     <xsl:variable name="vsignX">
  355.       <xsl:choose>
  356.         <xsl:when test="$pX >= 0">1</xsl:when>
  357.         <xsl:otherwise>-1</xsl:otherwise>
  358.       </xsl:choose>
  359.     </xsl:variable>
  360.     <xsl:variable name="vdiff" select="$pLength*floor($pX div $pLength) -$pX"/>
  361.     <xsl:choose>
  362.       <xsl:when test="$vdiff*$pX > 0">
  363.         <xsl:value-of select="$vsignX*$vdiff"/>
  364.       </xsl:when>
  365.       <xsl:otherwise>
  366.         <xsl:value-of select="-$vsignX*$vdiff"/>
  367.       </xsl:otherwise>
  368.     </xsl:choose>
  369.   </xsl:template>
  370.  
  371. </xsl:stylesheet>